import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:friends_flutter/utils/screens_utils.dart';
import 'package:friends_flutter/utils/ui.dart';
import 'color_utils.dart';

class TipDialogUtil {
  static show(
       {
        String title = '提示',
        String content = '内容',
        String leftText = '取消',
        String rightText = '确定',
        bool hideContent = false,
        bool hideCancel = false,
        Color bgColor = Colors.white,
        Color txtColor = ThemeColors.clff9f9f9f,
        Color titleColor = ThemeColors.clff202735,
        required Function okCallBack,
        Function? cancelCallBack,
      }) {
    showDialog(
      context: Get.context!,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return TipDialog(
          title: title,
          hideContent: hideContent,
          content: content,
          leftText: leftText,
          rightText: rightText,
          okCallBack: okCallBack,
          hideCancel: hideCancel,
          bgColor: bgColor,
          txtColor: txtColor,
          titleColor: titleColor,
          cancelCallBack: cancelCallBack,
        );
      },
    );
  }

  ///带确认、取消按钮提示框
  static show2({
        String content = '内容',
        String leftText = '取消',
        String rightText = '确定',
        required Function okCallBack,
        Function? cancelCallBack,
      }) {
    showDialog(
      context: Get.context!,
      builder: (BuildContext context) {
        return TipDialog(
          title: content,
          hideContent: true,
          content: '',
          leftText: leftText,
          rightText: rightText,
          okCallBack: okCallBack,
          hideCancel: false,
          bgColor: Colors.white,
          txtColor: ThemeColors.clff9f9f9f,
          titleColor: ThemeColors.clff202735,
          cancelCallBack: cancelCallBack,
        );
      },
    );
  }

  ///只有确认按钮的提示，点击蒙版弹窗消失
  static show3(
      BuildContext mContext, {
        String title = '内容',
        String rightText = '确定',
        required Function okCallBack,
      }) {
    showDialog(
      context: mContext,
      builder: (BuildContext context) {
        return TipDialog(
            title: title,
            hideContent: true,
            hideCancel: true,
            rightText: rightText,
            okCallBack: okCallBack);
      },
    );
  }

  ///确定和取消按钮上下排列
  static show4({
        String title = '标题',
        String rightText = '确定',
        String cancelText = '取消',
        bool hideCancel = false,
        required Function okCallBack,
      }) {
    showDialog(
      context: Get.context!,
      builder: (BuildContext context) {
        return TipDialog1(
            title: title,
            hideContent: true,
            hideCancel: hideCancel,
            cancelText: cancelText,
            rightText: rightText,
            okCallBack: okCallBack);
      },
    );
  }

}

class TipDialog extends Dialog {
  final String title; //标题
  final String content; //内容
  final String leftText; //左边按钮文字
  final String rightText; //右边按钮文字
  final Function okCallBack; //右边按钮回调
  final Function? cancelCallBack; //左边按钮回调
  final bool hideContent; //是否隐藏内容
  final bool hideCancel; //是否隐藏取消按钮
  final Color bgColor; //是否隐藏取消按钮
  final Color txtColor; //背景颜色
  final Color titleColor; //标题颜色

  const TipDialog(
      {this.title = '',
        this.hideContent = false,
        this.content = '',
        this.leftText = '',
        this.rightText = '',
        required this.okCallBack,
        this.cancelCallBack,
        this.hideCancel = false,
        this.bgColor = Colors.white,
        this.txtColor = Colors.black,
        this.titleColor = ThemeColors.clff202735,
        Key? key})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Material(
      //创建透明层
      type: MaterialType.transparency, //透明类型
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            width: double.infinity,
            margin: const EdgeInsets.symmetric(horizontal: 24),
            decoration:  BoxDecoration(
              color: bgColor,
              borderRadius: const BorderRadius.all(Radius.circular(16)),
            ),
            child: Column(
              children: [
                //是否隐藏标题
                Padding(
                  padding: EdgeInsets.symmetric(horizontal: 16, vertical: hideContent ? 34 : 20),
                  child: Text(title.isEmpty ? '温馨提示' : title,
                      style:  TextStyle(
                          color: titleColor,
                          fontSize: 20,
                          fontWeight: FontWeight.normal)),
                ),
               Offstage(
                  offstage: hideContent,
                  child: Padding(
                    padding: const EdgeInsets.only(left: 16, right: 16, bottom: 20),
                    child: Text(content,
                        textAlign: TextAlign.center,
                        style:
                        TextStyle(color: txtColor, fontSize: 16)),
                  ),
                ),
                HorizontalLine(color: ThemeColors.clff898da7, width: double.infinity,),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Offstage(
                      offstage: hideCancel,
                      child: GestureDetector(
                        onTap: () {
                          Navigator.of(context).pop();
                          if (cancelCallBack != null) {
                            cancelCallBack!.call();
                          }
                        },
                        child: Container(
                          alignment: Alignment.center,
                          height: 54,
                          width: (SCREENWIDTH() - 48)/2.0,
                          decoration: const BoxDecoration(
                            border: Border(right: BorderSide(width: 0.5, color: ThemeColors.clff898da7))
                          ),
                          child: Text(
                            leftText,
                            style: const TextStyle(color: ThemeColors.clff9f9f9f, fontSize: 16),
                          ),
                        ),
                      ),
                    ),
                    GestureDetector(
                      onTap: () {
                        Navigator.of(context).pop();
                        okCallBack.call();
                      },
                      child: Container(
                        alignment: Alignment.center,
                        height: 54,
                        width: (SCREENWIDTH() - 48)/2.0,
                        color: Colors.transparent,
                        child: Text(
                          rightText,
                          style: const TextStyle(color: ThemeColors.clff202735, fontSize: 16),
                        ),
                      ),
                    )
                  ],
                )
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class TipDialog1 extends Dialog {
  final String title; //标题
  final String content; //内容
  final String cancelText; //左边按钮文字
  final String rightText; //右边按钮文字
  final Function okCallBack; //右边按钮回调
  final Function? cancelCallBack; //左边按钮回调
  final bool hideContent; //是否隐藏内容
  final bool hideCancel; //是否隐藏取消按钮
  final Color? bgColor; //是否隐藏取消按钮
  final Color txtColor; //背景颜色
  final Color titleColor; //标题颜色

  const TipDialog1(
      {this.title = '',
        this.hideContent = false,
        this.content = '',
        this.cancelText = '',
        this.rightText = '',
        required this.okCallBack,
        this.cancelCallBack,
        this.hideCancel = false,
        this.bgColor,
        this.txtColor = Colors.black,
        this.titleColor = ThemeColors.clff202735,
        Key? key})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Material(
      //创建透明层
      type: MaterialType.transparency, //透明类型
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            width: double.infinity,
            margin: const EdgeInsets.symmetric(horizontal: 24),
            decoration:  BoxDecoration(
              color: bgColor,
              borderRadius: const BorderRadius.all(Radius.circular(16)),
              image: bgColor == null ? const DecorationImage(
                fit: BoxFit.fill,
                image: AssetImage(
                    'assets/images/my/tip_dialog_bg_show4.png', ),
              ) : null,
            ),
            child: Column(
              children: [
                //是否隐藏标题
                Padding(
                  padding: EdgeInsets.symmetric(horizontal: 40, vertical: hideContent ? 34 : 20),
                  child: Text(title.isEmpty ? '温馨提示' : title,
                      style:  TextStyle(
                          color: titleColor,
                          fontSize: 16,
                          fontWeight: FontWeight.normal), textAlign: TextAlign.center,),
                ),
                Offstage(
                  offstage: hideContent,
                  child: Padding(
                    padding: const EdgeInsets.only(left: 16, right: 16, bottom: 20),
                    child: Text(content,
                        textAlign: TextAlign.center,
                        style:
                        TextStyle(color: txtColor, fontSize: 16)),
                  ),
                ),
                Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    GestureDetector(
                      onTap: () {
                        Navigator.of(context).pop();
                        okCallBack.call();
                      },
                      child: Container(
                        height: 51,
                        width: 160,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(26),
                          color: ThemeColors.clff3a4bdd,
                        ),
                        child: Text(rightText, style: const TextStyle(fontSize: 20, color: ThemeColors.clffffffff),),
                      ),
                    ),
                    Offstage(
                      offstage: hideCancel,
                      child: GestureDetector(
                        onTap: () {
                          Navigator.of(context).pop();
                          if (cancelCallBack != null) {
                            cancelCallBack!.call();
                          }
                        },
                        child: Container(
                          height: 54,
                          alignment: Alignment.center,
                          color: Colors.transparent,
                          child: Text(cancelText, style: const TextStyle(fontSize: 16, color: ThemeColors.clff9f9f9f),),
                        ),
                      ),
                    ),
                    Space(height: 20,)
                  ],
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}


